package com.qwiki.test

import com.qwiki.xml._
import org.apache.log4j.{PatternLayout, ConsoleAppender, Logger}
import org.apache.log4j.helpers.DateLayout

/**
 * User: Xiaoyang Gu
 * Date: 11/4/12
 * Time: 1:43 AM
 */
object wikiXMLEngine2 {
  def main(args: Array[String]) {
    Logger.getRootLogger.addAppender(new ConsoleAppender(new PatternLayout("%d{yyyy/MM/dd HH:mm:ss.SSS} %p [%c{1}] [%x] %m%n")))

    val engine: WikiXmlEngine = new WikiXmlEngine(args(0));
    val numThreads:Int = java.lang.Integer.parseInt(args(1))
    val handlers: Array[WikiPageHandler] = new Array[WikiPageHandler](numThreads);
    for (i <- 0 until handlers.length) {
      handlers(i) = new WikiPageAsyncPrintHandler("test", i);
      handlers(i).asInstanceOf[WikiPageAsyncPrintHandler].start();
    }
    val handler: WikiPageHandler = new WikiPageMultiHandler(handlers);
    handler.setFilter(new WikiXMLPageValidArticleFilter());
    engine.setHandler(handler);
    engine.process();
    for (i <- 0 until handlers.length) {
      handlers(i).asInstanceOf[WikiPageAsyncPrintHandler].drain();
      println("%d drained".format(i))
      handlers(i).asInstanceOf[WikiPageAsyncPrintHandler].join();
    }
  }

}
